package com.vadmin.vadminjava.servlet;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.JwtUtil;
import com.vadmin.vadminjava.service.AdminConfigService;
import com.vadmin.vadminjava.service.AdminLogService;
import com.vadmin.vadminjava.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/******************************************************************************************
 *
 * 这个文件是打算放与项目实际业务无关的后台通用接口
 *
 *****************************************************************************************/

@RestController
@RequestMapping ("/AdminServlet")
public class AdminServlet {
	@Autowired
	AdminService         adminService;
	@Autowired
	AdminConfigService adminConfigService;
	@Autowired
	AdminLogService adminLogService;

	//用户登录
	@RequestMapping(value = {"/userLogin"})
	public String login(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		return adminService.userLogin(paramJ).toJSONString();
	}



	//获取系统配置列表
	@RequestMapping(value = {"/systemConfigList"})
	public String systemConfigList(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		return adminConfigService.systemConfigList(paramJ).toJSONString();
	}


	//更新系统配置
	@RequestMapping(value = {"/systemConfigUpdate"})
	public String systemConfigUpdate(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		return adminConfigService.systemConfigUpdate(paramJ).toJSONString();
	}


	//日志列表
	@RequestMapping(value = {"/logsList"})
	public String logsList(@RequestParam Map<String, Object> params) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));
		return adminLogService.logsList(paramJ).toJSONString();
	}



	////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////////////////////

	//jwt测试
	@RequestMapping(value = {"/jwtTest"})
	public String jwtTest(@RequestParam Map<String, Object> params,HttpServletRequest httpServletRequest) {
		JSONObject paramJ = new JSONObject();
		params.keySet().forEach(i -> paramJ.put(i, params.get(i)));

		String token = getToken(httpServletRequest);
		String tokenParse = JwtUtil.verify(token);
		JSONObject jwtObject = JSONObject.parseObject(tokenParse);
		System.out.println("解析的jwt JSONObject"+jwtObject);
		return tokenParse;
	}


	public String getToken(HttpServletRequest httpServletRequest){
		String token =  httpServletRequest.getHeader("token");
		return token;
	}
}
